package com.cskaoyan.shopping.cartcontroller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author doublec
 * @since 2022/10/25 16:22
 */
@RestController
public class CartController {
    @Autowired
    ICartService iCartService;

    @GetMapping("/shopping/carts")
    public ResponseData getCarts(HttpServletRequest request){
        //获取token信息
        //json字符串{"uid":1,"username":"zs"}
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        long uid = Long.parseLong(object.get("uid").toString());
        List<CartProductDto> cartListById = iCartService.getCartListById(uid);
        return new ResponseUtil().setData(cartListById);
    }

    @PostMapping("/shopping/carts")
    public ResponseData addCarts(@Validated @RequestBody CartRequest addCartRequest, HttpServletRequest request){
        //获取token信息
        //json字符串{"uid":1,"username":"zs"}
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        long uid = Long.parseLong(object.get("uid").toString());
        addCartRequest.setUserId(uid);
        Integer result = iCartService.addToCart(addCartRequest);
//        if (result == 0){
//            return new ResponseUtil<>().setErrorMsg(Integer.valueOf(ShoppingRetCode.STOCK_EMPTY.getCode()),ShoppingRetCode.STOCK_EMPTY.getMessage());
//        }
        if (result == 1){
            return new ResponseUtil<>().setErrorMsg(Integer.valueOf(ShoppingRetCode.OUT_RESTRICT_COUNT.getCode()),ShoppingRetCode.OUT_RESTRICT_COUNT.getMessage());
        }
        return new ResponseUtil().setData("成功","success");
    }

    @PutMapping("/shopping/carts")
    public ResponseData updateCarts(@Validated @RequestBody CartRequest updateCartRequest, HttpServletRequest request){
        //获取token信息
        //json字符串{"uid":1,"username":"zs"}
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        long uid = Long.parseLong(object.get("uid").toString());
        updateCartRequest.setUserId(uid);
        Integer result = iCartService.updateCartNum(updateCartRequest);
//        if (result == 0){
//            return new ResponseUtil<>().setErrorMsg(Integer.valueOf(ShoppingRetCode.STOCK_EMPTY.getCode()),ShoppingRetCode.STOCK_EMPTY.getMessage());
//        }
        if (result == 1){
            return new ResponseUtil<>().setErrorMsg(Integer.valueOf(ShoppingRetCode.OUT_RESTRICT_COUNT.getCode()),ShoppingRetCode.OUT_RESTRICT_COUNT.getMessage());
        }
        return new ResponseUtil().setData("成功","success");
    }

    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteCarts(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid){
        iCartService.deleteCartItem(uid,pid);
        return new ResponseUtil().setData("成功","success");
    }

    @DeleteMapping("/shopping/items/{uid}")
    public ResponseData deleteCheckedCarts(@PathVariable("uid") Long uid){
        iCartService.deleteCheckedItem(uid);
        return new ResponseUtil().setData("成功","success");
    }

    @PutMapping("/shopping/items")
    public ResponseData checkAllCarts(@Validated @RequestBody CheckAllItemRequest checkAllItemRequest, HttpServletRequest request){
        //获取token信息
        //json字符串{"uid":1,"username":"zs"}
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        long uid = Long.parseLong(object.get("uid").toString());
        checkAllItemRequest.setUserId(uid);
        iCartService.checkAllCartItem(checkAllItemRequest);
        return new ResponseUtil().setData("成功","success");
    }

}
